<?php
class Webkul_Marketplacepartner_Model_Userdata extends Mage_Core_Model_Abstract
{
	 public function _construct() {
        parent::_construct();
        $this->_init('marketplacepartner/userdata');
    }
	
	
	public function quantitySold($mageproid){
	$sum=0;
		$collection = Mage::getResourceModel('marketplacepartner/productsales_collection');
		$collection->getSelect()->where('mageproid ='.$mageproid);
		foreach($collection as $rec) {
		$sum=$sum+$rec->getmagequantity();
		}
		return $sum;
	}
	
	public function amountEarned($mageproid){
	$sum=0;
		$collection = Mage::getResourceModel('marketplacepartner/productsales_collection');
		$collection->getSelect()->where('mageproid ='.$mageproid);
		foreach($collection as $rec) {
		$sum=$sum+$rec->getactualparterprocost();
		}
		return $sum;
	}
	
	public function clearedAt($mageproid){
	$arr=array();
		$collection = Mage::getResourceModel('marketplacepartner/productsales_collection');
		$collection->getSelect()->where('mageproid ='.$mageproid);
		foreach($collection as $rec) {
		$arr[]=$rec->getcleared_at();
		}
		return $arr;
	}
	
	public function createdAt($mageproid){
	$arr=array();
	
		$collection = Mage::getModel('catalog/product')->getCollection();
		$collection->addFieldToFilter('entity_id',array('eq' => $mageproid));
		foreach($collection as $rec) {
		$arr[]=$rec->getCreatedAt();
		}
		return $arr;
	}
	
	public function approveSimpleProduct($id){
		$value=1;
		$customerProduct = Mage::getModel('marketplacepartner/marketplacepartner_product')->load($id);
		$magentoProductModel = Mage::getModel('catalog/product')->load($id);
		$user = Mage::getModel('marketplacepartner/userdata')->load($id);
		$magentoProductModel->setStatus(1);
		$saved=$magentoProductModel->save();
		$lastId = $saved->getId();
		$customerProduct->setProductId($lastId);
		$customerProduct->save();
		$write = Mage::getSingleton('core/resource')->getConnection('core_write');
		$querydata=$write->query("update ".Mage::getConfig()->getTablePrefix()."marketplacepartner_entity_data set status='".$value."' where mageproductid='".$id."'");
		
		$catagory_model = Mage::getModel('catalog/category');
		$categoriesy = $catagory_model->load($magentoProductModel->getCategoryIds());
		$categoryname=$categoriesy->getName();
		$partnerdata=$write->query("SELECT userid FROM ".Mage::getConfig()->getTablePrefix()."marketplacepartner_entity_data where customerpartnerproductid='".$id."'");
		$rowpartnerdata=$partnerdata->fetch();
		$userdata=$write->query("SELECT * FROM ".Mage::getConfig()->getTablePrefix()."customer_entity_varchar where attribute_id=5 and entity_id='".$rowpartnerdata['userid']."'");
		$rowuserdata=$userdata->fetch();
		$userdatat=$write->query("SELECT * FROM ".Mage::getConfig()->getTablePrefix()."customer_entity_varchar where attribute_id=7 and entity_id='".$rowpartnerdata['userid']."'");
		$rowuserdatat=$userdatat->fetch();
		$cfname='Administrator';
		$cmail='administrator@store.com';
		$email = Mage::getModel('customer/customer')->load($user->getUserid())->getemail();
		$headers = "From: STORE";
		$emailTemp = Mage::getModel('core/email_template')->loadDefault('whenproductapproved');
		$emailTempVariables = array();
		$emailTempVariables['myvar1'] = $magentoProductModel->getName();
		$emailTempVariables['myvar2'] =$magentoProductModel->getDescription();
		$emailTempVariables['myvar3'] =$magentoProductModel->getPrice();
		$emailTempVariables['myvar4'] =$categoryname;
		$emailTempVariables['myvar5'] =$rowuserdata['value'].' '.$rowuserdatat['value'];
		$processedTemplate = $emailTemp->getProcessedTemplate($emailTempVariables);
		$emailTemp->setSenderName($cfname);
		$emailTemp->setSenderEmail($cmail); 
		$emailTemp->send($email,$rowuserdata['value'].' '.$rowuserdatat['value'],$emailTempVariables);
	return $lastId;
	}
}